package com.wlper.lib.uitls;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间工具
 * 
 * @author wanliang527
 * 
 */
public class TimeUtils {
    public final static long MINUTE = 1000 * 60;
    public final static long HOUR = MINUTE * 60;
    public final static long DAY = HOUR * 24;
    public final static long MONTH = DAY * 30;
    public final static long YEAR = DAY * 365;

    public static String getCurrentTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss");
        return format.format(new Date());
    }

    /**
     * 计算距离给定时间的差，返回抽象的中文描述。例如“1年前”、“刚刚”...
     * 
     * @param lastTime
     *            通过System.currentTimeMillis()得到的毫秒数
     * @return
     */
    public static String computeTimeInterval(long lastTime) {
        long cur = System.currentTimeMillis();
        if (lastTime <= 0 || lastTime >= cur) {
            return "";
        }
        long l = cur - lastTime;
        if (l > YEAR) {
            return "1年前";
        } else if (l > MONTH) {
            long m = l / MONTH;
            return m + "个月前";
        } else if (l > DAY) {
            long d = l / DAY;
            return d + "天前";
        } else if (l > HOUR) {
            long h = l / HOUR;
            return h + "小时前";
        } else if (l > MINUTE) {
            long m = l / MINUTE;
            return m + "分钟前";
        } else {
            long s = l / 1000;
            if (s <= 5) {
                return "刚刚";
            } else {
                return s + "秒前";
            }
        }
    }
}
